﻿/**************************************************************************************************************
 * Class name:  T_LitJson.cs
 * Description: LitJson 测试页
 * Author:      Ju2ender
 * Create:      2012-11-19 Monday 16:18:16
 * Update:      2012-11-19 Monday 16:18:16
 **************************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LitJson;

namespace Web {

  public partial class T_LitJson : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

      if (!IsPostBack) {

        // Person to JSON
        Person person = null;
        string json = string.Empty;

        person = new Person { 
          Name = "Leehyo1ee",
          Age = DateTime.Now.Year - 1992,
          Birthday = new DateTime(1992, 9, 22)
        };

        Response.Write("Person:<br />");
        Response.Write(person.ToString() + "<br />");

        Response.Write("To JSON:<br />");
        json = JsonMapper.ToJson(person);
        Response.Write(json + "<br />");

        // JSON to Person
        Response.Write("To Person:<br />");
        person = JsonMapper.ToObject<Person>(json);
        Response.Write(person.ToString());
      }
    }
  }

  public class Person {

    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime Birthday { get; set; }

    public override string ToString() {
      return "Name: " + Name + ", " +
             "Age: " + Age.ToString() + ", " +
             "Birthday: " + Birthday.ToString() + ".";
    }
  }
}